package cn.lh.service.impl;

import cn.lh.contants.DeleteStateEnum;
import cn.lh.contants.ErrorCode;
import cn.lh.dao.OrderDao;
import cn.lh.entity.dto.OrderDto;
import cn.lh.entity.req.OrderListReq;
import cn.lh.entity.req.OrderReq;
import cn.lh.entity.vo.OrderVo;
import cn.lh.exception.BusinessException;
import cn.lh.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author linhong
 * @since 2025-04-14
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderDto> implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Override
    public void saveOrUpdate(OrderReq req) {
        OrderDto dto = req.toDto();
        this.saveOrUpdate(dto);
    }

    @Override
    public void delete(Integer orderId) {
        OrderDto dto = this.getById(orderId);
        if(dto == null){
            throw new BusinessException(ErrorCode.ORDER_NOT_EXISTS_ERROR);
        }
        dto.setDelState(DeleteStateEnum.DEL.getCode());
        this.updateById(dto);
    }

    @Override
    public PageInfo<OrderVo> getList(OrderListReq req) {
        PageHelper.startPage(req.getPageNum(),req.getPageSize());
        List<OrderVo> list = orderDao.getList(req);
        PageInfo<OrderVo> info = new PageInfo<>(list);
        return info;
    }
}
